//
//  ChooseWIFIType.m
//  Router
//
//  Created by teki on 15/10/28.
//  Copyright © 2015年 dc. All rights reserved.
//

#import "ChooseWIFITypeView.h"

@interface ChooseWIFITypeView ()
@property (nonatomic, copy) SelectedBlock block;

@property (weak, nonatomic) IBOutlet UIView *mBackgroundView;

@property (weak, nonatomic) IBOutlet UIView *mContentView;

@property (weak, nonatomic) IBOutlet UITableView *mTableView;
@property (nonatomic, strong) NSArray *dataArray;
@end

@implementation ChooseWIFITypeView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
+ (ChooseWIFITypeView *)loadViewFromNib
{
    NSArray *array = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
    if ([array count] >0) {
        return array[0];
    }
    return nil;
}


- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
    }
    return self;
}


- (void)awakeFromNib
{
    [self initUI];
}


- (void)initUI
{
    self.mBackgroundView.backgroundColor = [UIColor blackColor];
    self.mBackgroundView.alpha = 0.8;
    
    self.mContentView.layer.cornerRadius = 5.0f;
    self.mContentView.layer.masksToBounds = YES;
    self.dataArray = @[@"PPPOE宽带登录",@"固定IP登录",@"DHCP登录"];
    [self.mTableView setSeparatorInset:UIEdgeInsetsZero];
    [self.mTableView reloadData];
    
}


#pragma mark -
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"cellIdentifier";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text = self.dataArray[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.textColor = SeparatorLineColor;
    return cell;
}

#pragma mark -
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self hidden];
    if (self.block) {
        self.block(indexPath.row);
    }
    
}

// 设置分隔线 iOS8 下才起作用
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


- (void)show
{
    [self showInView:[UIApplication sharedApplication].keyWindow];
}


- (void)showInView:(UIView *)view
{
    [view addSubview:self];
    [self makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(view);
    }];

}


- (void)hidden
{
    [self removeFromSuperview];
}

- (IBAction)backgroundTap:(id)sender {
    [self hidden];
}



- (void)setSelectedBlock:(SelectedBlock)block
{
    _block = block;
}

@end
